**************************************************
*Yuhua Wang*
*The Rise and Fall of Imperial China: The Social Origins of State Development*
*Princeton University Press, 2022*
**************************************************

**************************************************
*Chapter 8*
**************************************************

**************************************************
*Figure 8.3: Lineage Activity Trends Before and After the Taiping Rebellion*
**************************************************

*Change this to your local directory*

cd "/Users/ywang/Dropbox/Book Project on State Building/Princeton UP/Replication Package/Chapter 8/data/"

set more off

use "Late Qing data.dta", clear

*treatment group in t-3*
mean genealogy_IHS if periodid==1 & massrebellion_dummy==1

*treatment group in t-2*
mean genealogy_IHS if periodid==2 & massrebellion_dummy==1

*treatment group in t-1*
mean genealogy_IHS if periodid==3 & massrebellion_dummy==1

*treatment group in t*
mean genealogy_IHS if periodid==4 & massrebellion_dummy==1

*treatment group in t+1*
mean genealogy_IHS if periodid==5 & massrebellion_dummy==1

*treatment group in t+2*
mean genealogy_IHS if periodid==6 & massrebellion_dummy==1

*control group in t-3*
mean genealogy_IHS if periodid==1 & massrebellion_dummy==0

*control group in t-2*
mean genealogy_IHS if periodid==2 & massrebellion_dummy==0

*control group in t-1*
mean genealogy_IHS if periodid==3 & massrebellion_dummy==0

*control group in t*
mean genealogy_IHS if periodid==4 & massrebellion_dummy==0

*control group in t+1*
mean genealogy_IHS if periodid==5 & massrebellion_dummy==0

*control group in t+2*
mean genealogy_IHS if periodid==6 & massrebellion_dummy==0

*estimate*

xi: reg genealogy_IHS post_massrebellion_dummy post i.countyid if periodid==3 | periodid==5,cl(countyid)
estimates store didestimate
estadd ysumm

esttab didestimate using didestimate.tex, ///
	replace fragment keep(post_massrebellion_dummy) ///
	b(3) se(3) posthead(\cmidrule(lr){2}) ///
	cells(b(star fmt(3)) se(fmt(3) par)) staraux star(* 0.10 ** 0.05 *** 0.01) collabels(none) ///
	label booktabs noconstant obslast nodepvars nomtitles nolines gaps ///
	scalars("r2 \(R^{2}\)") sfmt(3) ///
	stats(ymean ysd N r2, fmt(3 3 0 3) labels("Outcome mean" "Outcome std.dev." "Observations" "R2")) ///
	indicate("County FE = *countyid*" "Period FE = post")

clear

*copy the following data into Stata:

period	treat	genealogy_IHS	se
1	1	0.3340609	0.0617809
2	1	0.4785693	0.0735889
3	1	0.5919341	0.0828938
4	1	0.6766695	0.0860142
5	1	0.9674155	0.10927
6	1	1.067555	0.1172632
1	0	0.1446313	0.0114981
2	0	0.1859186	0.013677
3	0	0.2299175	0.0152454
4	0	0.2591332	0.0165087
5	0	0.3530268	0.0198015
6	0	0.4427217	0.0221518
1	2	0.3340609	0.0617809
2	2	0.4785693	0.0735889
3	2	0.5225682	0.0828938
4	2	0.5517839	0.0860142
5	2	0.6456775	0.10927
6	2	0.7353724	0.1172632

gen upper95ci=genealogy_ihs+1.96*se
gen lower95ci=genealogy_ihs-1.96*se

format genealogy_ihs %02.1f

label define period 1 "1790-1809" 2 "1810-1829" 3 "1830-1849" 4 "1850-1869" 5 "1870-1889" 6 "1890-1910"

save "didestimates.dta", replace

use "didestimates.dta", clear

*95% CI*

  twoway (scatter genealogy_ihs period if treat==1 & period>1, msymbol(circle) mcolor(red) msize(small)) ///
  (line genealogy_ihs period if treat==1 & period>1, lpattern(dash) lcolor(red) lwidth(medthick)) ///
  (scatter genealogy_ihs period if treat==0 & period>1, msymbol(circle) mcolor(blue) msize(small)) ///
  (line genealogy_ihs period if treat==0 & period>1, lpattern(solid) lcolor(blue) lwidth(medthick)) ///
  (scatter genealogy_ihs period if treat==2 & period>1, msymbol(circle) mcolor(blue) msize(small)) ///
  (line genealogy_ihs period if treat==2 & period>1, lpattern(dot) lcolor(blue) lwidth(medthick)), ///
  legend(off) scheme(lean1) graphregion(fcolor(white) lcolor(white)) ///
  xlab(2 "1810-1829" 3 "1830-1849" 4 "1850-1869" 5 "1870-1889" 6 "1890-1909", valuelabel) ///
   ylab(0(0.2)1.2, nogrid) yline(0, lcolor(black) lpattern(dash)) ///
   ytitle("N of Genealogy Books (IHS)") xtitle("Period") xscale(range(1.5,6.5)) ///
   text(0.35 6 "Control Group") text(1.15 6 "Treatment Group") text(0.65 6 "Counterfactual") ///
   text(0.8 5 "0.252***",orient(vertical)) xline(4,lcolor(gs14) lwidth(vvvthick))
 
